package com.suray.wcs.web.system.controller;


import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.enums.CodeLevel;
import com.suray.wcs.service.enums.CodeType;
import com.suray.wcs.service.pojo.BaseEum;
import com.suray.wcs.service.pojo.Code;
import com.suray.wcs.service.system.entity.CodeDB;
import com.suray.wcs.service.system.service.impl.CodeDBServiceImpl;
import com.suray.wcs.service.util.ExcelUtilPlus;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 故障码 前端控制器
 * </p>
 *
 * @author cooper
 * @since 2019-06-26
 */
@RestController
@RequestMapping("/exceptionCode")
@CrossOrigin(value = "*", allowCredentials = "true")
public class CodeDBController extends BaseController {
    @Autowired
    private CodeDBServiceImpl codedbServiceImpl;

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation(value = "分页查询故障码")
    @PreAuthorize("@ss.hasPermi('exceptionCode:list')")
    @GetMapping(value = "/list")
    @ResponseBody
    public TableDataInfo getCodeList(CodeDB codeDB) {
        startPage();
        List<CodeDB> result = codedbServiceImpl.selectCodeList(codeDB);
        return getDataTable(result);
    }

    @ApiOperation("导出字典数据")
    @Log(title = "异常字典", method = "导出字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('exceptionCode:export')")
    @GetMapping("/export")
    public ResponseData export(CodeDB codeDB) {
        List<CodeDB> list = codedbServiceImpl.selectCodeList(codeDB);
        ExcelUtilPlus<CodeDB> util = new ExcelUtilPlus<CodeDB>(CodeDB.class);
        return util.exportExcel(list, "异常字典数据");
    }

    /**
     * 导出模板
     *
     * @return
     */
    @ApiOperation("导出模板")
    @GetMapping("/importTemplate")
    public ResponseData importTemplate() {
        ExcelUtilPlus<CodeDB> util = new ExcelUtilPlus<CodeDB>(CodeDB.class);
        return util.importTemplateExcel("异常字典数据");
    }

    @ApiOperation("导入字典数据")
    @Log(title = "异常字典", method = "导入字典数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('exceptionCode:import')")
    @PostMapping("/importData")
    public ResponseData importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtilPlus<CodeDB> util = new ExcelUtilPlus<CodeDB>(CodeDB.class);
        List<CodeDB> codeDBList = util.importExcel(file.getInputStream());
        String message = codedbServiceImpl.importCode(codeDBList, updateSupport);
        return ResponseData.success(message);
    }

    /**
     * 删除
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('code:remove')")
    @Log(title = "异常字典", method = "删除异常码", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除异常码")
    @DeleteMapping(value = "/deleteCode/{codeIds}")
    public ResponseData deleteCode(@PathVariable Integer codeIds) {
        return toAjax(codedbServiceImpl.deleteCodeByIds(codeIds));
    }
    /**
     * 新增
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('exceptionCode:add')")
    @Log(title = "异常字典", method = "新增故障码", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增故障码")
    @PostMapping(value = "/addCode")
    @ResponseBody
    public ResponseData addCodeList(Code code) {
        CodeDB codedb = TransformDb(code);
        boolean result = codedbServiceImpl.save(codedb);
        if (result) {
            return ResponseData.success("添加成功");
        }
        return ResponseData.error("");
    }

    /**
     * 修改
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('exceptionCode:edit')")
    @Log(title = "异常字典", method = "修改故障码", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改故障码")
    @PutMapping(value = "/editCode")
    @ResponseBody
    public ResponseData editCodeList(Code code) {
        CodeDB codedb = TransformDb(code);
        boolean result = codedbServiceImpl.updateById(codedb);
        if (result) {
            return ResponseData.success("修改成功");
        }
        return ResponseData.error("修改失败");
    }

    /**
     * 修改异常码
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('code:edit')")
    @Log(title = "异常字典", method = "修改异常码", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改异常码")
    @PutMapping(value = "/updateCode")
    public ResponseData updateCode(@Validated @RequestBody CodeDB codeDB) {
        if (codedbServiceImpl.checkCodeUnique(codeDB)) {
            return ResponseData.error("修改异常码'" + codeDB.getCodeDec() + "'失败, 异常码已存在");
        }
        return toAjax(codedbServiceImpl.updateById(codeDB));
    }

    /**
     * 获取异常码
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('code:query')")
    @ApiOperation(value = "获取异常码")
    @GetMapping(value = "/getCode/{codeId}")
    public ResponseData getCode(@PathVariable Integer codeId) {
        return ResponseData.success(codedbServiceImpl.getById(codeId));
    }

    /**
     * 获取故障码类型
     *
     * @return
     */
    @ApiOperation(value = "获取故障码类型")
    @GetMapping(value = "/getCodeType")
    @ResponseBody
    public ResponseData getCodeType() {
        List<CodeType> codeTypes = Arrays.asList(CodeType.values());
        List<BaseEum> codEums = new ArrayList<BaseEum>();
        for (CodeType codeType : codeTypes) {
            codEums.add(new BaseEum(codeType.getValue(), codeType.toString()));
        }
        return ResponseData.success("查询完成", codEums);
    }

    /**
     * 获取故障码级别
     *
     * @return
     */
    @ApiOperation(value = "获取故障码级别")
    @GetMapping(value = "/getCodeLevel")
    @ResponseBody
    public ResponseData getCodeLevel() {
        List<CodeLevel> codeLevels = Arrays.asList(CodeLevel.values());
        List<BaseEum> codEums = new ArrayList<BaseEum>();
        for (CodeLevel codeLevel : codeLevels) {
            codEums.add(new BaseEum(codeLevel.getValue(), codeLevel.toString()));
        }
        return ResponseData.success("查询完成", codEums);
    }


    @Log(title = "操作日志", method = "清空小车心跳日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('rgvHeartLog:remove')")
    @DeleteMapping("/clean")
    public ResponseData clean() {
        codedbServiceImpl.deleteAllCode();
        return ResponseData.success();
    }

    /**
     * 前后端交互对象转换成DB对象
     * 我妥协了 szn
     *
     * @param code
     * @return
     */
    private CodeDB TransformDb(Code code) {
        CodeDB codedb = new CodeDB();
        BeanUtils.copyProperties(code, codedb);
        Integer codeDec = null;
        if (code.getCodeDec() != null && !"".equals(code.getCodeDec())) {
            try {
                codeDec = Integer.parseInt(code.getCodeDec());
            } catch (Exception e) {
                codeDec = -1;
            }
        }
        codedb.setCodeDec(codeDec);
        return codedb;
    }
}
